/** ***********************************************************************************
* @file         fota.c
* @brief        升级部分
* @details      
* @author       
* @date         2021/3
* @copyright    北京高知科技有限公司    
* @version      V1.0
* @attention    无
**************************************************************************************/
#define OSI_LOG_TAG OSI_MAKE_LOG_TAG('F', 'O', 'T', 'A')

#include "cJSON.h"
#include "os_app.h"
#include "log_task.h"
#include "n808_app.h"
#include "md5.h"
#include "vehicle_data_process.h"
#include "files.h"
#include "algorithm.h"
#include "mangpatio.h"
#include "fota.h"

GaozSemaphoreHandle_t   fota_mutex = 0; //互斥

ota_msg_t ota_msg={0}; /*global*/

//static void fota_app(void *param);
//static void fota_firmware(void *param);
//static void fota_http(void *param);
/** ****************************************************************************
* @remarks      static void mcuvertoformate(UINT8 *pASCStr,uint8_t *outstr)
* @brief         
* @param[in]     
* @param[out]    
* @return        无
* @attention     无
*******************************************************************************/
static void mcuvertoformate(UINT8 *pASCStr,uint8_t *outstr)
{
	int idx;

	idx = 0;
	memcpy(outstr,pASCStr,2);  //15
	idx += 2;
	outstr[idx++] = '.';
	memcpy(&outstr[idx],&pASCStr[2],2);//00
	idx += 2;
	outstr[idx++] = '.';
	memcpy(&outstr[idx],&pASCStr[4],4);//0001
	idx += 4;
	outstr[idx++] = '.';
	memcpy(&outstr[idx],&pASCStr[8],4);//0001
	idx += 4;
	outstr[idx++] = '.';
	memcpy(&outstr[idx],&pASCStr[12],6);//001012
	idx += 6;
	outstr[idx] = '\0';	
}
/** ****************************************************************************
* @remarks       static void cb_http_response_handle(void *pHttpParam, INT8 *data, INT32 len)
* @brief         http 响应回调
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void cb_http_response_handle(void *pHttpParam, INT8 *data, INT32 len)
{
    fibo_taskSleep(1000);
    fibo_http_response_status_line((oc_http_param_t *)pHttpParam);

    gaoz_log_debug_printf(INFOR_MODE,"fota","@@@ HTTP response code          : %d", ((oc_http_param_t *)pHttpParam)->respCode);
    gaoz_log_debug_printf(INFOR_MODE,"fota","@@@ HTTP response version       : %s", ((oc_http_param_t *)pHttpParam)->respVersion);
    gaoz_log_debug_printf(INFOR_MODE,"fota","@@@ HTTP response reason phrase : %s", ((oc_http_param_t *)pHttpParam)->respReasonPhrase);
    gaoz_log_debug_printf(INFOR_MODE,"fota","@@@ HTTP response reason phrase : %s", ((oc_http_param_t *)pHttpParam)->respReasonPhrase);
    gaoz_log_debug_printf(INFOR_MODE,"fota","@@@ HTTP Recive Data : %d %s", len, data);
}
/** ****************************************************************************
* @remarks       static void cb_http_header_handle(const UINT8 *name, const UINT8 *value)
* @brief         http header回调
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void cb_http_header_handle(const UINT8 *name, const UINT8 *value)
{
    gaoz_log_debug_printf(INFOR_MODE,"fota","Header: %s : %s", name, value);
}

/** ****************************************************************************
* @remarks       static void cb_http_header_handle(const UINT8 *name, const UINT8 *value)
* @brief        
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
static void split_softwareversion(uint8_t *in,uint16_t len,ver_property_e part, l610_class_e class, uint8_t *out)
{
    uint16_t size = len;
    uint8_t i = 0;
    uint8_t *p = in;

    if(in == NULL)
    {
        return;
    }

    while(size)
    {
        if(*p == '.')
        {
            i++;
        }
        if((i == 3) && (part == VER_MOTORCYCLE))
        {
            memcpy(out,++p,6);
            break;
        }
        else if((i == 4) && (part == VER_HARDWARE))
        {
            if(class == L610_APP)
            {
                memcpy(out,++p,4);
                break;
            }
            else if(class == L610_MCU)
            {
                memcpy(out,++p,6);
                break;
            }
        }
        else if((i == 5) && (part == VER_FIRMWARE))
        {
            memcpy(out,++p,2);
            break;
        }
        p++;
        size--;
    }
}

/** ****************************************************************************
* @remarks       void fota_init( void )
* @brief         资源初始化
* @param[in]     无
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void fota_init( void )
{
    fota_mutex = gaoz_mutex_create();
    if( fota_mutex == 0 )
    {
        while(1); /*fail*/
    }
}

/** ****************************************************************************
* @remarks       int cjson_parseurl(char *da, uint16_t len)
* @brief         解析平台升级链接
* @param[in]     *da 获取的数据地址
* @param[in]     len 长度 pv不定向
* @param[out]    无
* @return        1 成功 0 失败 
* @attention     无
*******************************************************************************/
int cjson_parseurl(char *da, uint16_t len, upgrade_url_e url_type)
{
    cJSON *cjson;
    INT32 ret = 0;
    INT8 message[50] = {0};
    uint8_t localver[20] = {0};
    uint8_t server[20] = {0};
    uint8_t mcuverasc[18] = {0};
    uint8_t mcuverasc_form[50] = {0};

    //ota_msg_t ota_msg={0};
    update_result_t upgrade_msg;
    
    sprintf(message, "%s", fibo_get_sw_verno());
    
    char *pt = (char *)gaoz_fibo_malloc(1024*2);
    if (pt == NULL)
    {
       return 0;
    }
    if(len != 0)
    {
        memcpy(pt,da,len);
    }
    else
    {
        return 0;
    }
    cjson = cJSON_Parse(pt);
    if(cjson == NULL)
    {
        gaoz_log_debug_printf(INFOR_MODE,"fota","json pack into cjson error...");
        gaoz_fibo_free(pt);
        return 0;
    }
    else
    {
        INT8 *content = cJSON_Print(cjson);
        if(content != NULL)
        {
            gaoz_log_debug_printf(INFOR_MODE,"fota","cJSON_ParseUrl content = %s\n", content);
        }
        cJSON_free(content);
    }

    cJSON *md5 = cJSON_GetObjectItem(cjson,"md5");
    cJSON *version = cJSON_GetObjectItem(cjson,"version");
    cJSON *tag = cJSON_GetObjectItem(cjson,"tag");
    cJSON *url = cJSON_GetObjectItem(cjson,"url");

    if(md5 && md5->valuestring \
       && version && version->valuestring \
       && tag && tag->valuestring \
       && url && url->valuestring)
       {
            gaoz_log_debug_printf(INFOR_MODE,"fota", "md5 %s ",md5->valuestring);    
            memcpy(ota_msg.md5,md5->valuestring,strlen(md5->valuestring));  
            memset(ota_msg.version,0,sizeof(ota_msg.version));
            gaoz_log_debug_printf(INFOR_MODE,"fota", "version %s ",version->valuestring);  
            memcpy(ota_msg.version,version->valuestring,strlen(version->valuestring));
            memset(ota_msg.tag,0,sizeof(ota_msg.tag));
            gaoz_log_debug_printf(INFOR_MODE,"fota", "tag %s ",tag->valuestring);  
            memcpy(ota_msg.tag,tag->valuestring,strlen(tag->valuestring));   
            gaoz_log_debug_printf(INFOR_MODE,"fota", "url %s ",url->valuestring);  
            gaoz_log_debug_printf(INFOR_MODE,"fota", "terota_handle.upgrade_type %d,upgrade_url %d\r\n ",terota_handle.upgrade_type,url_type);  
            if(get_mcuversion() != 0)
            {
                hexStr2ascii(get_mcuversion(),mcuverasc,9);
                mcuvertoformate(mcuverasc,mcuverasc_form);    
                gaoz_log_debug_printf(INFOR_MODE,"fota", "mcuverasc_form %s\r\n",mcuverasc_form);  
            }
            
            if((memcmp(ota_msg.version,message,strlen(message)) != 0)&&(memcmp(ota_msg.tag,"module_l610self",strlen("module_l610self")) == 0))  
            {
                gaoz_mutex_lock(fota_mutex);
                
                if(terota_handle.upgrade_type == OTA_INIT)
                {
                    terota_handle.upgrade_type = OTA_FIRMWARE;
                    terota_handle.upgrade_url = url_type;
                    memset(terota_handle.serveradr,0,sizeof(terota_handle.serveradr));
                    memcpy(terota_handle.serveradr,url->valuestring,strlen(url->valuestring));
                    memset(terota_handle.version,0,sizeof(terota_handle.version));
                    memcpy(terota_handle.version,version->valuestring,strlen(version->valuestring)); 
                    memset(terota_handle.md5,0,sizeof(terota_handle.md5));
                    memcpy(terota_handle.md5,md5->valuestring,strlen(md5->valuestring));
                    terota_handle.chk_xor = get_chk_xor((uint8_t *)&terota_handle,sizeof(terminal_ota_t)-1);
                    
                    ret = file_termpara_write(TERMOTA,(INT8 *)&terota_handle,sizeof(terminal_ota_t)/*strlen(url->valuestring)+1+strlen(md5->valuestring)+1*/);
                    if(ret < 0)
                    {
                          gaoz_log_debug_printf(INFOR_MODE,"fota", "file_termpara_write OTA_FIRMWARE fail!\r\n ");       
                    }
                }
                gaoz_mutex_unlock(fota_mutex);
                //gaoz_thread_create(fota_firmware, FOTA_FIRMWARE_NAME, 1024 * 16, /*&ota_msg*/NULL, OSI_PRIORITY_NORMAL);    
            }
            else if((memcmp(ota_msg.version,gz_sw_verno,strlen(gz_sw_verno)) != 0)&&(memcmp(ota_msg.tag,"module_l610app",strlen("module_l610app")) == 0))
            {
                gaoz_mutex_lock(fota_mutex);
                split_softwareversion((uint8_t *)gz_sw_verno,strlen(gz_sw_verno),VER_HARDWARE,L610_APP,localver);
                split_softwareversion((uint8_t *)ota_msg.version,strlen((char *)ota_msg.version),VER_HARDWARE,L610_APP,server);
                gaoz_log_debug_printf(INFOR_MODE,"fota", "localhwver[%s],serhwver[%s]\r\n ",localver,server);    
                if(memcmp(localver,server,4) == 0)
                {
                    if(terota_handle.upgrade_type == OTA_INIT)
                    {
                        terota_handle.upgrade_type = OTA_APP;
                        terota_handle.upgrade_url = url_type;
                        memset(terota_handle.serveradr,0,sizeof(terota_handle.serveradr));
                        memcpy(terota_handle.serveradr,url->valuestring,strlen(url->valuestring));
                        memset(terota_handle.version,0,sizeof(terota_handle.version));
                        memcpy(terota_handle.version,version->valuestring,strlen(version->valuestring)); 
                        memset(terota_handle.md5,0,sizeof(terota_handle.md5));
                        memcpy(terota_handle.md5,md5->valuestring,strlen(md5->valuestring));
                        terota_handle.chk_xor = get_chk_xor((uint8_t *)&terota_handle,sizeof(terminal_ota_t)-1);
                        
                        ret = file_termpara_write(TERMOTA,(INT8 *)&terota_handle,sizeof(terminal_ota_t)/*saveData,strlen(url->valuestring)+1+strlen(md5->valuestring)+1*/);
                        if(ret < 0)
                        {
                            gaoz_log_debug_printf(INFOR_MODE,"fota", "file_termpara_write OTA_APP fail!\r\n ");       
                        }
                    }
                    gaoz_mutex_unlock(fota_mutex);
                }
                else
                { 
                    gaoz_fibo_free(pt);
                    cJSON_Delete(cjson); 
                    gaoz_mutex_unlock(fota_mutex); 
                    return 0;  
                }
                //gaoz_thread_create(fota_app, FOTA_APP_NAME, 1024 * 16, /*&ota_msg*/NULL, OSI_PRIORITY_NORMAL);
            }
            else if((mcuverasc_form != 0)&&(memcmp(ota_msg.tag,"module_vehicles",strlen("module_vehicles")) == 0))
            {
                gaoz_mutex_lock(fota_mutex);
                if(memcmp(ota_msg.version,mcuverasc_form,strlen((char *)mcuverasc_form)) != 0)
                {
                    split_softwareversion((uint8_t *)mcuverasc_form,strlen((char *)mcuverasc_form),VER_HARDWARE,L610_MCU,localver);
                    split_softwareversion((uint8_t *)ota_msg.version,strlen((char *)ota_msg.version),VER_HARDWARE,L610_MCU,server);
                    gaoz_log_debug_printf(INFOR_MODE,"fota", "localhwver[%s],serhwver[%s]\r\n ",localver,server);  
                    if(memcmp(localver,server,6) == 0)
                    { 
                        if(terota_handle.upgrade_type == OTA_INIT)
                        {
                            terota_handle.upgrade_type = OTA_VEHICLE;
                            terota_handle.upgrade_url = url_type;
                            memset(terota_handle.serveradr,0,sizeof(terota_handle.serveradr));
                            memcpy(terota_handle.serveradr,url->valuestring,strlen(url->valuestring));
                            memset(terota_handle.version,0,sizeof(terota_handle.version));
                            memcpy(terota_handle.version,version->valuestring,strlen(version->valuestring)); 
                            memset(terota_handle.md5,0,sizeof(terota_handle.md5));
                            memcpy(terota_handle.md5,md5->valuestring,strlen(md5->valuestring));
                            terota_handle.chk_xor = get_chk_xor((uint8_t *)&terota_handle,sizeof(terminal_ota_t)-1);
                        
                            ret = file_termpara_write(TERMOTA,(INT8 *)&terota_handle,sizeof(terminal_ota_t)/*saveData,strlen(url->valuestring)+1+strlen(md5->valuestring)+1*/);
                            if(ret < 0)
                            {
                                gaoz_log_debug_printf(INFOR_MODE,"fota", "file_termpara_write OTA_VEHICLE fail!\r\n ");       
                            }
                        }
                    }
                    gaoz_mutex_unlock(fota_mutex);
                }
                else
                { 
                        gaoz_fibo_free(pt);
                        cJSON_Delete(cjson); 
                        gaoz_mutex_unlock(fota_mutex); 
                        return 0;  
                }             
                //gaoz_thread_create(fota_http, FOTA_HTPP_NAME, 1024 * 10, /*(ota_msg_t *)&ota_msg*/NULL, OSI_PRIORITY_NORMAL);
            } 
            else
            {
                //錯誤
                gaoz_log_debug_printf(INFOR_MODE,"fota", "ver not change! "); 
                gaoz_fibo_free(pt);
                cJSON_Delete(cjson);  
                if(url_type == URL_MAJOR)
                {
                    upgrade_msg.update_result = 1;
                    upgrade_msg.update_types = 0;
                    ret = national808_upgrade_queue_put(&upgrade_msg);
                    if(ret < 1)
                    {
                        gaoz_log_debug_printf(INFOR_MODE,"fota", "national808_upgrade_queue_put fail\r\n");  
                    }
                }
                else
                {
                    upgrade_msg.update_result = 1;
                    upgrade_msg.update_types = 0;
                    ret = mangpatio_upgrade_queue_put(&upgrade_msg);
                    if(ret < 1)
                    {
                        gaoz_log_debug_printf(INFOR_MODE,"fota", "mangpatio_upgrade_queue_put fail\r\n");  
                    }
                }
                
                return 0;
            }
        }
        else
        {
            gaoz_fibo_free(pt);
            cJSON_Delete(cjson);  
            upgrade_msg.update_result = 1;
            upgrade_msg.update_types = 0;
            if(url_type == URL_MAJOR)
            {
                ret = national808_upgrade_queue_put(&upgrade_msg);
                if(ret < 1)
                {
                    gaoz_log_debug_printf(INFOR_MODE,"fota", "national808_upgrade_queue_put fail\r\n");  
                }
            }
            else
            {
                ret = mangpatio_upgrade_queue_put(&upgrade_msg);
                if(ret < 1)
                {
                    gaoz_log_debug_printf(INFOR_MODE,"fota", "mangpatio_upgrade_queue_put fail\r\n");  
                }
            }
            
            return 0;
        }
          
    gaoz_fibo_free(pt);
    cJSON_Delete(cjson);  
    gaoz_log_debug_printf(INFOR_MODE,"fota","cJSON_Parse url abn!!!!!!");

    return 1;        
}

/** ****************************************************************************
* @remarks      void fota_firmware(void *param)
* @brief        固件升级 
* @param[in]     *param
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
/*
static void fota_firmware(void *param)
{
 	int ret = 0;
    
    //if( param == NULL )				//空指针返回
	//{
    //    gaoz_mutex_unlock(fota_mutex);
	//	return;
	//}
    //ota_msg_t *otamsg = (ota_msg_t *)(param);
    
    if(ota_msg.serveradr != 0)
    {
        gaoz_log_debug_printf(INFOR_MODE,"fota","otaData.ServerAdr[%s]",ota_msg.serveradr);
        fibo_set_ssl_chkmode(0);

        ret = fibo_firmware_ota(0, (UINT8 *)ota_msg.serveradr, NULL, NULL, NULL);
        gaoz_log_debug_printf(INFOR_MODE,"fota","ret = %d", ret);
        if (ret < 0)
        {
            gaoz_log_debug_printf(INFOR_MODE,"fota","firmware upgrade fail!");
        }
    }   
    gaoz_mutex_unlock(fota_mutex);
    fibo_thread_delete();	
}
*/
/** ****************************************************************************
* @remarks      void fota_app(void *param)
* @brief        APP升级 
* @param[in]     *param
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
/*
static void fota_app(void *param)
{
    int ret = 0;
    
    //if( param == NULL )				//空指针返回
	//{
    //    gaoz_mutex_unlock(fota_mutex);
	//	return;
	//}
    //ota_msg_t *otamsg = (ota_msg_t *)(param);
    
   if(ota_msg.serveradr != 0)
    {
        gaoz_log_debug_printf(INFOR_MODE,"fota","otaData.ServerAdr[%s]",ota_msg.serveradr);
        fibo_set_ssl_chkmode(0);
        ret = fibo_open_ota(0, (UINT8 *)ota_msg.serveradr, NULL, NULL, NULL);
        gaoz_log_debug_printf(INFOR_MODE,"fota","ret = %d", ret);
        if (ret < 0)
        {
            gaoz_log_debug_printf(INFOR_MODE,"fota","ota upgrade fail!");
        }
    }
    gaoz_mutex_unlock(fota_mutex);
    fibo_thread_delete();
}
*/
/** ****************************************************************************
* @remarks      static INT32 http_file_md5(const INT8 *pPath,void *pHttpParam,uint8_t *data)
* @brief        文件校验md5
* @param[in]     
* @param[out]    无
* @return        -1 失败， 0 成功
* @attention     无
*******************************************************************************/
static INT32 http_file_md5(const INT8 *pPath,void *pHttpParam,uint8_t *data)
{
    INT32 fd = 0;
    MD5_CTX ctx;
    int len = 0;
    int val = 0;
    uint8_t outputmd[16]={0};
    uint8_t outputmdASC[32] = {0};     

    if(data == NULL)
    {
        return -1;
    }
    char *pt = (char *)fibo_malloc(1024*2);
    if (pt == NULL)
    {
        return -1;
    }
    fd = fibo_file_open(pPath, O_RDWR);
    if(fd == -1)
    {
        gaoz_log_debug_printf(INFOR_MODE,"fota","%s,%d http file read error", __FUNCTION__, __LINE__);
        fibo_file_close(fd);
        fibo_free(pt);
        return -1;
    }
    /*MD5 check*/
    MD5_Init(&ctx);
    while((len=fibo_file_read(fd, (UINT8 *)pt, 1024))>0)
    {
        MD5_Update(&ctx,(unsigned char *)pt,len);
        memset(pt,0,1024);
    }
    MD5_Final(outputmd,&ctx);
   
    hexStr2ascii(outputmd,outputmdASC,16);
    gaoz_log_debug_printf(INFOR_MODE,"fota","read md5:%s\r\n",outputmdASC );
    gaoz_log_debug_printf(INFOR_MODE,"fota","md5_GD:%s\r\n",data );
    if(memcmp(data,outputmdASC,32)==0)
    {
        val = 0;
    }
    else
    {
        val = -1;
    }
    fibo_file_close(fd);
    fibo_free(pt);   
    return val;
}

/** ****************************************************************************
* @remarks      void fota_http(void *param)
* @brief        http升级 
* @param[in]     *param
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
/*
static void fota_http(void *param)
{
    int ret = 0;
    const char *filepath = MCU_HTTP_FILE;
    oc_http_param_t * pstHttpParam = NULL;
    //ota_mcu_request_t req_msg; 
    update_result_t upgrade_msg;

    
    //if( param == NULL )				//空指针返回
	//{
    //    gaoz_mutex_unlock(fota_mutex);
	//	return;
	//}
    //ota_msg_t *otamsg = (ota_msg_t *)(param);
    

    gaoz_log_debug_printf(INFOR_MODE,"fota","otaData.ServerAdr[%s]",ota_msg.serveradr);
    gaoz_log_debug_printf(INFOR_MODE,"fota","otaData.md5_GD[%s]",ota_msg.md5);

    fibo_set_ssl_chkmode(0);

	pstHttpParam = fibo_http_new();
    if(NULL == pstHttpParam)
    {
        goto EXIT;
    }
    // prepare http get param 
    pstHttpParam->timeout = 60;
    pstHttpParam->cbReadBody = (http_read_body_callback)cb_http_response_handle;
    pstHttpParam->respCode = 500;
    pstHttpParam->bIsIpv6 = false;
    pstHttpParam->enHttpReadMethod = OC_SAVE_FILE;
    if(strlen(filepath) <= OC_HTTP_FILE_PATH_LEN)
    {
        strncpy((char *)pstHttpParam->filepath, filepath, strlen(filepath));
    }
    //pstHttpParam->filepath[strlen(filepath)] = '\0';

    memset(pstHttpParam->url, 0, OC_HTTP_URL_LEN + 1);
    if(strlen((char *)ota_msg.serveradr) <= OC_HTTP_URL_LEN)
    {
        strncpy((char *)pstHttpParam->url, (char *)ota_msg.serveradr, strlen((char *)ota_msg.serveradr));
    }      
    else
    {
        goto EXIT;
    }
    fibo_http_get(pstHttpParam, NULL);
    fibo_taskSleep(1000);
		
    gaoz_log_debug_printf(INFOR_MODE,"fota","HTTP response code          : %d", pstHttpParam->respCode);
    gaoz_log_debug_printf(INFOR_MODE,"fota","HTTP response version       : %s", pstHttpParam->respVersion);
    gaoz_log_debug_printf(INFOR_MODE,"fota","HTTP response reason phrase : %s", pstHttpParam->respReasonPhrase);
    gaoz_log_debug_printf(INFOR_MODE,"fota","HTTP response body len      : %d", pstHttpParam->respContentLen);
   
    fibo_http_response_header_foreach(pstHttpParam, cb_http_header_handle);


    if(OC_SAVE_FILE == pstHttpParam->enHttpReadMethod)
    { 
           if(http_file_md5(filepath,pstHttpParam,ota_msg.md5) == 0)
           {
                vehicle_ota_sem_set();
                mcu_ota_cle_overnum();
            }
            else
            {
               //error
                gaoz_log_debug_printf(INFOR_MODE,"fota"," MD5 error!!!!"); 
                upgrade_msg.update_result = 1;
                upgrade_msg.update_types = 0;
                ret = national808_upgrade_queue_put(&upgrade_msg);
                if(ret < 1)
                {
                    gaoz_log_debug_printf(INFOR_MODE,"fota", "national808_upgrade_queue_put fail\r\n");  
                }  
                gaoz_task_sleep(100);
            }    
    }
    fibo_http_delete(pstHttpParam);
    pstHttpParam = NULL;
    gaoz_log_debug_printf(INFOR_MODE,"fota"," http delete!!!!\r\n");  
    gaoz_task_sleep(200); 
EXIT:
    gaoz_mutex_unlock(fota_mutex);
    fibo_thread_delete();  
}*/
/** ****************************************************************************
* @remarks      static uint32_t http_manage(terminal_ota_t *ota_handle)
* @brief        http处理 
* @param[in]     *ota_handle升级类型句柄
* @param[out]    无
* @return        pdFAIL失败 pdPASS成功
* @attention     无
*******************************************************************************/
static uint32_t http_manage(terminal_ota_t *ota_handle)
{
    int ret = 0;
    const char *filepath = MCU_HTTP_FILE;
    oc_http_param_t * pstHttpParam = NULL;
    update_result_t upgrade_msg;

	pstHttpParam = fibo_http_new();
    if(NULL == pstHttpParam)
    {
        return pdFAIL;
    }
    /* prepare http get param */
    pstHttpParam->timeout = 60;
    pstHttpParam->cbReadBody = (http_read_body_callback)cb_http_response_handle;
    pstHttpParam->respCode = 500;
    pstHttpParam->bIsIpv6 = false;
    pstHttpParam->enHttpReadMethod = OC_SAVE_FILE;
    if(strlen(filepath) <= OC_HTTP_FILE_PATH_LEN)
    {
        strncpy((char *)pstHttpParam->filepath, filepath, strlen(filepath));
    }

    memset(pstHttpParam->url, 0, OC_HTTP_URL_LEN + 1);
    if(strlen((char *)ota_handle->serveradr) <= OC_HTTP_URL_LEN)
    {
        strncpy((char *)pstHttpParam->url, (char *)ota_handle->serveradr, strlen((char *)ota_handle->serveradr));
    }      
    else
    {
        fibo_http_delete(pstHttpParam);
        return pdFAIL;
    }
    fibo_http_get(pstHttpParam, NULL);
    fibo_taskSleep(1000);
		
    gaoz_log_debug_printf(INFOR_MODE,"fota","HTTP response code          : %d", pstHttpParam->respCode);
    gaoz_log_debug_printf(INFOR_MODE,"fota","HTTP response version       : %s", pstHttpParam->respVersion);
    gaoz_log_debug_printf(INFOR_MODE,"fota","HTTP response reason phrase : %s", pstHttpParam->respReasonPhrase);
    gaoz_log_debug_printf(INFOR_MODE,"fota","HTTP response body len      : %d", pstHttpParam->respContentLen);
   
    fibo_http_response_header_foreach(pstHttpParam, cb_http_header_handle);


    if(OC_SAVE_FILE == pstHttpParam->enHttpReadMethod)
    { 
           if(http_file_md5(filepath,pstHttpParam,ota_handle->md5) == 0)
           {
                mcu_ota_cle_overnum();
                vehicle_ota_sem_set();
                mcu_ota_set_status();
            }
            else
            {
               //error
                gaoz_log_debug_printf(INFOR_MODE,"fota"," MD5 error!!!!"); 
                upgrade_msg.update_result = 1;
                upgrade_msg.update_types = 0;
                ret = national808_upgrade_queue_put(&upgrade_msg);
                if(ret < 1)
                {
                    gaoz_log_debug_printf(INFOR_MODE,"fota", "national808_upgrade_queue_put fail\r\n");  
                }  
                fibo_http_delete(pstHttpParam);
                return pdFAIL;
            }    
    }
    fibo_http_delete(pstHttpParam);
    pstHttpParam = NULL;
    gaoz_log_debug_printf(INFOR_MODE,"fota"," http delete!!!!\r\n");  

    return pdPASS;
}

/** ****************************************************************************
* @remarks      void fota_manage(void *param)
* @brief        升级 接口调用 
* @param[in]     *ota_handle句柄
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void fota_manage(terminal_ota_t *ota_handle)
{
    int ret = 0;
    
    fibo_set_ssl_chkmode(0);
    if(ota_handle->upgrade_type == OTA_FIRMWARE)
    {
        ret = fibo_firmware_ota(0, (UINT8 *)ota_handle->serveradr, NULL, NULL, NULL);
        gaoz_log_debug_printf(INFOR_MODE,"fota","ret = %d", ret);
        if (ret == 0)
        {
            ota_handle->upgrade_type = OTA_INIT;
            ota_handle->chk_xor = get_chk_xor((uint8_t *)ota_handle,sizeof(terminal_ota_t)-1);
            ret = file_termpara_write(TERMOTA,(INT8 *)ota_handle,sizeof(terminal_ota_t));
            if(ret < 0)
            {
                gaoz_log_debug_printf(INFOR_MODE,"fota", "[%s-%d]file_termpara_write OTA_FIRMWARE fail!\r\n ",__FUNCTION__, __LINE__);       
            }
        }
        else
        {
            ota_handle->upgrade_type = OTA_INIT;
            gaoz_log_debug_printf(INFOR_MODE,"fota","firmware upgrade fail!");
        } 
    }
    else if(ota_handle->upgrade_type == OTA_APP)
    {
        ret = fibo_open_ota(0, (UINT8 *)ota_handle->serveradr, NULL, NULL, NULL);
        gaoz_log_debug_printf(INFOR_MODE,"fota","ret = %d", ret);
        if (ret == 0)
        {
            ota_handle->upgrade_type = OTA_INIT;
            ota_handle->chk_xor = get_chk_xor((uint8_t *)ota_handle,sizeof(terminal_ota_t)-1);
            ret = file_termpara_write(TERMOTA,(INT8 *)ota_handle,sizeof(terminal_ota_t));
            if(ret < 0)
            {
                gaoz_log_debug_printf(INFOR_MODE,"fota", "[%s-%d]file_termpara_write OTA_APP fail!\r\n ",__FUNCTION__, __LINE__);       
            }
        }
        else
        {
            ota_handle->upgrade_type = OTA_INIT;
            gaoz_log_debug_printf(INFOR_MODE,"fota","app upgrade fail!");
        } 
    }  
    else if(ota_handle->upgrade_type == OTA_VEHICLE)
    {
        if(http_manage(ota_handle) == pdPASS)
        {
            ota_handle->upgrade_type = OTA_INIT;
            ota_handle->chk_xor = get_chk_xor((uint8_t *)ota_handle,sizeof(terminal_ota_t)-1);
            ret = file_termpara_write(TERMOTA,(INT8 *)ota_handle,sizeof(terminal_ota_t));
            if(ret < 0)
            {
                gaoz_log_debug_printf(INFOR_MODE,"fota", "[%s-%d]file_termpara_write OTA_VEHICLE fail!\r\n ",__FUNCTION__, __LINE__);       
            }
        }
        else
        {
            ota_handle->upgrade_type = OTA_INIT;
            gaoz_log_debug_printf(INFOR_MODE,"fota","vehicle upgrade fail!");
        }
    }
}

/** ****************************************************************************
* @remarks      void fota_checkversion(void)
* @brief        升级保存数据校验 
* @param[in]     
* @param[out]    无
* @return        无
* @attention     无
*******************************************************************************/
void fota_checkversion(void)
{
    int ret = 0;
    char message[50] = {0};
    
    sprintf(message, "%s", fibo_get_sw_verno());
    ret = file_termpara_read(TERMOTA,(INT8 *)&terota_handle);//bak ota
    gaoz_log_debug_printf(INFOR_MODE,"fota", "terota_handle.version=%s  upgrade_type=[%d] \r\n ", terota_handle.version,terota_handle.upgrade_type );       
    if(ret < 0)
	{
		memset((uint8_t *)&terota_handle , 0x00 , sizeof(terminal_ota_t));
	}
    if((terota_handle.upgrade_type == OTA_FIRMWARE) && 
       (memcmp(terota_handle.version,message,strlen(message)) == 0 ))
    {
        terota_handle.upgrade_type = OTA_INIT;
        terota_handle.chk_xor = get_chk_xor((uint8_t *)&terota_handle,sizeof(terminal_ota_t)-1);
        ret = file_termpara_write(TERMOTA,(INT8 *)&terota_handle,sizeof(terminal_ota_t));
        if(ret < 0)
        {
            gaoz_log_debug_printf(INFOR_MODE,"fota", "[%s-%d]file_termpara_write OTA_FIRMWARE fail!\r\n ",__FUNCTION__, __LINE__);       
        }
    }
    else if((terota_handle.upgrade_type == OTA_APP) && 
       (memcmp(terota_handle.version,gz_sw_verno,strlen(gz_sw_verno)) == 0 ))
    {
        gaoz_log_debug_printf(INFOR_MODE,"fota", "debug ota          gz_sw_verno :%s\r\n ",gz_sw_verno); 
        gaoz_log_debug_printf(INFOR_MODE,"fota", "debug ota terota_handle.version:%s\r\n ",terota_handle.version); 
        terota_handle.upgrade_type = OTA_INIT;
        terota_handle.chk_xor = get_chk_xor((uint8_t *)&terota_handle,sizeof(terminal_ota_t)-1);
        ret = file_termpara_write(TERMOTA,(INT8 *)&terota_handle,sizeof(terminal_ota_t));
        if(ret < 0)
        {
            gaoz_log_debug_printf(INFOR_MODE,"fota", "[%s-%d]file_termpara_write OTA_APP fail!\r\n ",__FUNCTION__, __LINE__);       
        }
    }
    else if((terota_handle.upgrade_type == OTA_VEHICLE) && 
       (memcmp(terota_handle.version,get_mcuversion,strlen((char *)get_mcuversion)) == 0 ))
    {
        terota_handle.upgrade_type = OTA_INIT;
        terota_handle.chk_xor = get_chk_xor((uint8_t *)&terota_handle,sizeof(terminal_ota_t)-1);
        ret = file_termpara_write(TERMOTA,(INT8 *)&terota_handle,sizeof(terminal_ota_t));
        if(ret < 0)
        {
            gaoz_log_debug_printf(INFOR_MODE,"fota", "[%s-%d]file_termpara_write OTA_VEHICLE fail!\r\n ",__FUNCTION__, __LINE__);       
        }
    }    
}
